package com.android.cattlefarm.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.android.cattlefarm.util.ExportEntity;
import com.android.cattlefarm.util.ImportEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Huang on 2015/7/4.
 */
public class AddDB {

    public static String ADD_TABLE = "add_table";

    public static String TIQURONGLIANG = "tiqurongliang"; //体躯容量
    public static String KAOBU = "kaobu";//尻部
    public static String ZHITI = "zhiti";//肢蹄
    public static String QIANRUFANG = "qianrufang"; //前乳房
    public static String HOURUFANG = "hourufang";//后乳房
    public static String XITONG = "xitong"; //乳房系统
    public static String TEZHENG = "tezheng";//乳用特征
    public static String AVEGARE = "avegare";//平均
    public static String COLUMN_VALUE = "column_value";//选中数据

    SQLiteDatabase data;
    DBBaseHelper helper;
    Context context;
    Object object = new Object();

    public AddDB(Context context) {
        if (helper == null) {
            helper = DBBaseHelper.getInstance(context);
        }
        data = helper.getWritableDatabase();
        this.context = context;

    }

    /**
     * 添加
     *
     * @param entities
     * @return
     */
    public long insertImport(List<ExportEntity> entities) {
        this.data.delete(ADD_TABLE, null, null);
        //this.data.delete(AddDB.ADD_TABLE, null, null);
        long i = 0;
        data.beginTransaction();
        for (ExportEntity exportEntity : entities) {
            ContentValues values = new ContentValues();
            values.put(ImportDB.COW_NUM, exportEntity.getCowNum());
            values.put(ImportDB.COW_FARM_NUM, exportEntity.getCowfarmNum());
            values.put(ImportDB.COW_FARM_NAME, exportEntity.getCowfarmName());
            values.put(ImportDB.BIRTHDAY, exportEntity.getBirthday());
            values.put(ImportDB.TYRE, exportEntity.getTyre());
            values.put(ImportDB.PRODUCTION_TIME, exportEntity.getProductionTime());
            values.put(ImportDB.FATHER, exportEntity.getFather());
            values.put(ImportDB.MOTHER, exportEntity.getMother());
            values.put(ImportDB.GRANDFATER, exportEntity.getGrandfather());
            values.put(TIQURONGLIANG, exportEntity.getTiqurongliang());
            values.put(KAOBU, exportEntity.getKaobu());
            values.put(ZHITI, exportEntity.getZhiti());
            values.put(QIANRUFANG, exportEntity.getQianrufang());
            values.put(HOURUFANG, exportEntity.getHourufang());
            values.put(XITONG, exportEntity.getXitong());
            values.put(TEZHENG, exportEntity.getRuyongtezheng());
            values.put(AVEGARE, exportEntity.getAverage());
            values.put(COLUMN_VALUE, exportEntity.getPointString());
            i = this.data.insert(ADD_TABLE, null, values);
        }
        data.setTransactionSuccessful();
        data.endTransaction();
        closeDataBase();
        return i;
    }
    /**
     * insert
     * @param exportEntity
     * @return
     */
    public int insertExport(ExportEntity exportEntity) {
        long i = 0L;
        if (!existsExport(exportEntity.getCowNum())) {
            ContentValues values = new ContentValues();
            values.put(ImportDB.COW_NUM, exportEntity.getCowNum());
            values.put(ImportDB.COW_FARM_NUM, exportEntity.getCowfarmNum());
            values.put(ImportDB.COW_FARM_NAME, exportEntity.getCowfarmName());
            values.put(ImportDB.BIRTHDAY, exportEntity.getBirthday());
            values.put(ImportDB.TYRE, exportEntity.getTyre());
            values.put(ImportDB.PRODUCTION_TIME, exportEntity.getProductionTime());
            values.put(ImportDB.FATHER, exportEntity.getFather());
            values.put(ImportDB.MOTHER, exportEntity.getMother());
            values.put(ImportDB.GRANDFATER, exportEntity.getGrandfather());
            values.put(TIQURONGLIANG, exportEntity.getTiqurongliang());
            values.put(KAOBU, exportEntity.getKaobu());
            values.put(ZHITI, exportEntity.getZhiti());
            values.put(QIANRUFANG, exportEntity.getQianrufang());
            values.put(HOURUFANG, exportEntity.getHourufang());
            values.put(XITONG, exportEntity.getXitong());
            values.put(TEZHENG, exportEntity.getRuyongtezheng());
            values.put(AVEGARE, exportEntity.getAverage());
            values.put(COLUMN_VALUE, exportEntity.getPointString());
            i = data.insert(ADD_TABLE, null, values);
        } else {
            i = modifyExport(exportEntity);
        }

        return (int) i;
    }

    /**
     * modify
     *
     * @param
     * @return
     */
    public int modifyExport(ExportEntity exportEntity) {
        long i = 0L;
        ContentValues values = new ContentValues();
        values.put(ImportDB.COW_NUM, exportEntity.getCowNum());
        values.put(ImportDB.COW_FARM_NUM, exportEntity.getCowfarmNum());
        values.put(ImportDB.COW_FARM_NAME, exportEntity.getCowfarmName());
        values.put(ImportDB.BIRTHDAY, exportEntity.getBirthday());
        values.put(ImportDB.TYRE, exportEntity.getTyre());
        values.put(ImportDB.PRODUCTION_TIME, exportEntity.getProductionTime());
        values.put(ImportDB.FATHER, exportEntity.getFather());
        values.put(ImportDB.MOTHER, exportEntity.getMother());
        values.put(ImportDB.GRANDFATER, exportEntity.getGrandfather());
        values.put(TIQURONGLIANG, exportEntity.getTiqurongliang());
        values.put(KAOBU, exportEntity.getKaobu());
        values.put(ZHITI, exportEntity.getZhiti());
        values.put(QIANRUFANG, exportEntity.getQianrufang());
        values.put(HOURUFANG, exportEntity.getHourufang());
        values.put(XITONG, exportEntity.getXitong());
        values.put(AVEGARE, exportEntity.getAverage());
        values.put(TEZHENG, exportEntity.getRuyongtezheng());
        values.put(COLUMN_VALUE, exportEntity.getPointString());
        i = data.update(ADD_TABLE, values, ImportDB.COW_NUM  + "=?", new String[]{exportEntity.getCowNum()});
        return (int) i;
    }

    public ArrayList<ExportEntity> queryExportList() {
        ArrayList<ExportEntity> entities = new ArrayList<>();
        String sql = "select * from "+ ADD_TABLE;
        Cursor c = this.data.rawQuery(sql, null);
        if (c != null && c.getCount() > 0) {
            while (c.moveToNext()) {
                ExportEntity entity = new ExportEntity();
                entity.setCowNum(c.getString(c.getColumnIndex(ImportDB.COW_NUM)));
                entity.setCowfarmNum(c.getString(c.getColumnIndex(ImportDB.COW_FARM_NUM)));
                entity.setCowfarmName(c.getString(c.getColumnIndex(ImportDB.COW_FARM_NAME)));
                entity.setBirthday(c.getString(c.getColumnIndex(ImportDB.BIRTHDAY)));
                entity.setTyre(c.getString(c.getColumnIndex(ImportDB.TYRE)));
                entity.setProductionTime(c.getString(c.getColumnIndex(ImportDB.PRODUCTION_TIME)));
                entity.setFather(c.getString(c.getColumnIndex(ImportDB.FATHER)));
                entity.setGrandfather(c.getString(c.getColumnIndex(ImportDB.GRANDFATER)));
                entity.setMother(c.getString(c.getColumnIndex(ImportDB.MOTHER)));

                entity.setRuyongtezheng(c.getFloat(c.getColumnIndex(TEZHENG)));
                entity.setTiqurongliang(c.getFloat(c.getColumnIndex(TIQURONGLIANG)));
                entity.setZhiti(c.getFloat(c.getColumnIndex(ZHITI)));
                entity.setQianrufang(c.getFloat(c.getColumnIndex(QIANRUFANG)));
                entity.setHourufang(c.getFloat(c.getColumnIndex(HOURUFANG)));
                entity.setXitong(c.getFloat(c.getColumnIndex(XITONG)));
                entity.setAverage(c.getFloat(c.getColumnIndex(AVEGARE)));
                entity.setKaobu(c.getFloat(c.getColumnIndex(KAOBU)));
                entity.toPoint(c.getString(c.getColumnIndex(COLUMN_VALUE)));
                entities.add(entity);
            }
        }
        c.close();
        return entities;
    }
    /**
     * judge exists
     *
     * @param cowNum
     * @return
     */
    private boolean existsExport(String cowNum) {
        Cursor c = data.query(ADD_TABLE, null, ImportDB.COW_NUM + "=?", new String[]{cowNum}, null, null, null);
        if (c != null && c.getCount() > 0) {
            c.close();
            return true;
        }
        c.close();
        return false;
    }

    /**
     * 关闭数据库
     */
    public void closeDataBase() {
        if (data != null) {
            data.close();
        }
    }
}
